package com.example.jserrorresolver.controller;

import com.example.jserrorresolver.model.ErrorStackRequest;
import com.example.jserrorresolver.model.ErrorStackResponse;
import com.example.jserrorresolver.service.SourceMapResolverService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/error")
@Slf4j
@RequiredArgsConstructor
public class ErrorStackController {

    private final SourceMapResolverService resolverService;

    @PostMapping("/resolve-stack")
    public ResponseEntity<ErrorStackResponse> resolveErrorStack(@RequestBody ErrorStackRequest request) {
        try {
            log.info("收到错误堆栈解析请求");
            String resolvedStack = resolverService.resolveErrorStack(
                    request.getStackTrace(),
                    request.getSourceMapDirectory()
            );
            return ResponseEntity.ok(new ErrorStackResponse(true, resolvedStack, null));
        } catch (Exception e) {
            log.error("解析错误堆栈失败", e);
            return ResponseEntity.ok(new ErrorStackResponse(
                    false,
                    null,
                    "解析失败: " + e.getMessage()
            ));
        }
    }
}
